﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.Net;
using System.Collections;
using System.Web.Security;
using System.Runtime.InteropServices;


namespace iResume
{
    public partial class forgetpassword : System.Web.UI.Page
    {  
       
        //generate random password
        //create constant strings for each type of characters
        static string alphaCaps = "QWERTYUPASDFGHJKLZXCVBNM";
        static string alphaLow = "qwertyupasdfghjklzxcvbnm";
        static string numerics = "1234567890";
        static string special = "@#$!%";
        //create another string which is a concatenation of all above
        string allChars = alphaCaps + alphaLow + numerics + special;
        Random r = new Random();

        public string GenerateStrongPassword(int length)
        {
            String generatedPassword = "";

            if (length < 4)
                throw new Exception("Number of characters should be greater than 4.");

            // Generate four repeating random numbers are postions of
            // lower, upper, numeric and special characters
            // By filling these positions with corresponding characters,
            // we can ensure the password has atleast one
            // character of those types
            int pLower, pUpper, pNumber, pSpecial;
            string posArray = "0123456789";
            if (length < posArray.Length)
                posArray = posArray.Substring(0, length);
            pLower = getRandomPosition(ref posArray);
            pUpper = getRandomPosition(ref posArray);
            pNumber = getRandomPosition(ref posArray);
            pSpecial = getRandomPosition(ref posArray);


            for (int i = 0; i < length; i++)
            {
                if (i == pLower)
                    generatedPassword += getRandomChar(alphaCaps);
                else if (i == pUpper)
                    generatedPassword += getRandomChar(alphaLow);
                else if (i == pNumber)
                    generatedPassword += getRandomChar(numerics);
                else if (i == pSpecial)
                    generatedPassword += getRandomChar(special);
                else
                    generatedPassword += getRandomChar(allChars);
            }
            return generatedPassword;
        }

        private string getRandomChar(string fullString)
        {
            return fullString.ToCharArray()[(int)Math.Floor(r.NextDouble() * fullString.Length)].ToString();
        }

        private int getRandomPosition(ref string posArray)
        {
            int pos;
            string randomChar = posArray.ToCharArray()[(int)Math.Floor(r.NextDouble()
                                           * posArray.Length)].ToString();
            pos = int.Parse(randomChar);
            posArray = posArray.Replace(randomChar, "");
            return pos;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //emailtxb.Text = (String)(Session["username"]);
        }

        protected void pro_Click(object sender, EventArgs e)
        {
    
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Session["email"] = emailtxb.Text;
            string temp = GenerateStrongPassword(8);
            Session["usertypes"] = ddl.SelectedValue;
            string email = (string)(Session["Email"]);

            if (ddl.SelectedValue == "Job Seeker")
            {
                bool result = DBManager.jsforgetpassword(emailtxb.Text);

                if (result == true)
                {



                    //create the message
                    MailMessage mail = new MailMessage();
                    //add the email address we will be sending the message to
                    mail.To.Add(email);
                    //add our email here
                    mail.From = new MailAddress("gladysyiwei@hotmail.com");
                    //email's subject
                    mail.Subject = "Confirmation Password";
                    //email's body, this is going to be html. note that we attach the image as using cid
                    mail.Body = "Hi" + " " + email + "(Job Seeker)" + ",<br>" +
                        "<br>You have requested to reset your account password. To complete your request, please <br>" +
                        "<br>enter the following confirmation code:<br>" +temp+
                         "<br>Please note: for your protection, a copy of this email has been sent to all the email addresses associated with your account.";
                    //set email's body to html
                    mail.IsBodyHtml = true;

                    //add our attachment
                    //Attachment imgAtt = new Attachment(Server.MapPath("re.gif"));
                    //give it a content id that corresponds to the src we added in the body img tag
                    //imgAtt.ContentId = "re.gif";
                    //add the attachment to the email
                    //mail.Attachments.Add(imgAtt);

                    //setup our smtp client, these are Gmail specific settings
                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                    client.EnableSsl = true; //ssl must be enabled for Gmail
                    //our Gmail account credentials
                    NetworkCredential credentials = new NetworkCredential("iResumee@gmail.com","yiweileeling");
                    //add credentials to our smtp client
                    client.Credentials = credentials;

                    try
                    {
                        //try to send the mail message
                        client.Send(mail);
                    }
                    catch
                    {
                        //some feedback if it does not work
                        Messagelbl.Text = "Fail";
                    }

                    JobSeeker js = new JobSeeker(temp,email);
  
                    DBManager.jsupdateconfirmcode(js);

                   
      
                    FormsAuthentication.RedirectFromLoginPage(emailtxb.Text, false);
                    Response.Redirect("confirmation.aspx");

                }
                else
                {
                    Messagelbl.Text = " usertype or email is incorrect ";
                }
            }

            if (ddl.SelectedValue == "Recruiter")
            {
                bool result1 = DBManager.rforgetpassword(emailtxb.Text);

                if (result1 == true)
                {



                    //create the message
                    MailMessage mail = new MailMessage();
                    //add the email address we will be sending the message to
                    mail.To.Add(email);
                    //add our email here
                    mail.From = new MailAddress("gladysyiwei@hotmail.com");
                    //email's subject
                    mail.Subject = "Confirmation Password";
                    //email's body, this is going to be html. note that we attach the image as using cid
                    mail.Body = "Hi" + " " + email + "(Recruiter)" +",<br>" +
                        "<br>You have requested to reset your account password. To complete your request, please <br>" +
                        "<br>enter the following confirmation code:<br>" + temp +
                         "<br>Please note: for your protection, a copy of this email has been sent to all the email addresses associated with your account.";
                    //set email's body to html
                    mail.IsBodyHtml = true;

                    //add our attachment
                    //Attachment imgAtt = new Attachment(Server.MapPath("re.gif"));
                    //give it a content id that corresponds to the src we added in the body img tag
                    //imgAtt.ContentId = "re.gif";
                    //add the attachment to the email
                    //mail.Attachments.Add(imgAtt);

                    //setup our smtp client, these are Gmail specific settings
                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                    client.EnableSsl = true; //ssl must be enabled for Gmail
                    //our Gmail account credentials
                    NetworkCredential credentials = new NetworkCredential("iResumee@gmail.com", "yiweileeling");
                    //add credentials to our smtp client
                    client.Credentials = credentials;

                    try
                    {
                        //try to send the mail message
                        client.Send(mail);
                    }
                    catch
                    {
                        //some feedback if it does not work
                        Messagelbl.Text = "Fail";
                    }

                 //   JobSeeker js = new JobSeeker(temp, email);
                    Recruiter r = new Recruiter(email, temp);

                    DBManager.rupdateconfirmcode(r);



                    FormsAuthentication.RedirectFromLoginPage(emailtxb.Text, false);
                    Response.Redirect("confirmation.aspx");

                }
                else
                {
                    Messagelbl.Text = " usertype or email is incorrect ";
                }
            }

            if (ddl.SelectedValue == "Administrator")
            {
                bool result = DBManager.adminforgetpassword(emailtxb.Text);

                if (result == true)
                {



                    //create the message
                    MailMessage mail = new MailMessage();
                    //add the email address we will be sending the message to
                    mail.To.Add(email);
                    //add our email here
                    mail.From = new MailAddress("gladysyiwei@hotmail.com");
                    //email's subject
                    mail.Subject = "Confirmation Password";
                    //email's body, this is going to be html. note that we attach the image as using cid
                    mail.Body = "Hi" + " " + email + "(Administrator)" + ",<br>" +
                        "<br>You have requested to reset your account password. To complete your request, please <br>" +
                        "<br>enter the following confirmation code:<br>" + temp +
                         "<br>Please note: for your protection, a copy of this email has been sent to all the email addresses associated with your account.";
                    //set email's body to html
                    mail.IsBodyHtml = true;

                    //add our attachment
                    //Attachment imgAtt = new Attachment(Server.MapPath("re.gif"));
                    //give it a content id that corresponds to the src we added in the body img tag
                    //imgAtt.ContentId = "re.gif";
                    //add the attachment to the email
                    //mail.Attachments.Add(imgAtt);

                    //setup our smtp client, these are Gmail specific settings
                    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
                    client.EnableSsl = true; //ssl must be enabled for Gmail
                    //our Gmail account credentials
                    NetworkCredential credentials = new NetworkCredential("iResumee@gmail.com", "yiweileeling");
                    //add credentials to our smtp client
                    client.Credentials = credentials;

                    try
                    {
                        //try to send the mail message
                        client.Send(mail);
                    }
                    catch
                    {
                        //some feedback if it does not work
                        Messagelbl.Text = "Fail";
                    }

                   // JobSeeker js = new JobSeeker(temp, email);
                    Admin a = new Admin(email, temp);

                    DBManager.aupdateconfirmcode(a);



                    FormsAuthentication.RedirectFromLoginPage(emailtxb.Text, false);
                    Response.Redirect("confirmation.aspx");

                }
                else
                {
                    Messagelbl.Text = " usertype or email is incorrect ";
                }
            }



     

        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("createJobseeker.aspx");
        }
    }
}